package com.micromall.common.utils;


import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
/**
 * HttpClient Utilities
 * 
 * @author yong.ma
 * @since 2.0
 */

/**
 * <p> 类说明 ：text/application-json</p>
 * @author LIXIAOFENG
 * Date: 2016年10月8日
 * @version 1.01
 *
 */
public abstract class MyHttpUtils {

	public static CloseableHttpClient createDefaultSsLClient() {
		try {
			SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null,
					new TrustStrategy() {
						// 信任所有
						public boolean isTrusted(X509Certificate[] chain, String authType)
								throws CertificateException {
							return true;
						}
					}).build();
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
			return HttpClients.custom().setSSLSocketFactory(sslsf).build();
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	public static byte[] httpsGet0(String url) {
		try {
			CloseableHttpClient httpClient = createDefaultSsLClient();
			HttpGet get = new HttpGet();
			get.setURI(new URI(url));
			HttpResponse response = httpClient.execute(get);
			// 获取响应实体
			HttpEntity entity = response.getEntity();
			if (null == entity) {
				return new byte[0];
			}
			byte[] datas = IOUtils.toByteArray(entity.getContent());
			EntityUtils.consume(entity); // Consume response content
			return datas;
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	public static byte[] httpsPost0(String url, String content, String charsetName) throws UnsupportedEncodingException {
		return httpsPost0(url, new StringEntity(content, charsetName));
	}

	public static byte[] httpsPost0(String url, List<? extends NameValuePair> pairs, String charset) {
		try {
			return httpsPost0(url, new UrlEncodedFormEntity(pairs, charset));
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}

	public static byte[] httpsPost0(String url, HttpEntity content) {
		try {
			CloseableHttpClient httpClient = createDefaultSsLClient();
			HttpPost post = new HttpPost();
			post.setURI(new URI(url));
			post.setEntity(content);
			post.setHeader("Content-Type", "application/json");
			HttpResponse response = httpClient.execute(post);
			// 获取响应实体
			HttpEntity entity = response.getEntity();
			if (null == entity) {
				return new byte[0];
			}
			byte[] datas = IOUtils.toByteArray(entity.getContent());
			EntityUtils.consume(entity); // Consume response content
			return datas;
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	public static String httpsGet(String url, String charsetName) {
		try {
			return new String(httpsGet0(url), charsetName);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	public static String httpsPost(String url, String content, String charsetName) {
		try {
			return new String(httpsPost0(url, content, charsetName), charsetName);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	public static String httpsPost(String url, List<? extends NameValuePair> pairs, String charset) {
		try {
			return new String(httpsPost0(url, pairs, charset), charset);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}
}
